﻿using System;
using System.Xml.Serialization;
using System.Web.Script.Serialization;

using Fuse8.DomainFramework.Domain;
using Fuse8.DomainFramework.BusinessService;
using Fuse8.DomainFramework.CacheService;

namespace Fuse8.ContentManagementFramework.Domain
{
    [Serializable]
    public partial class CmsEntityMetaFieldLink : Int32EntityObject
    {
        #region Linked fields

        private CmsEntityMetaField _entityMetaField;

        #endregion

        #region Properties

        public virtual int EntityMetaFieldId { get; set; }

        public virtual string EntityTypeParameter { get; set; }

        public virtual string DisplayLabel { get; set; }

        public virtual bool IsAllowMultipleValues { get; set; }

        public virtual string MetaFieldValues { get; set; }  
        
        public virtual bool IsRequired { get; set; }

        public virtual int MetaFieldOrder { get; set; }

        public virtual string EditorName { get; set; }

        public virtual string DefaultValue { get; set; }

        #endregion

        #region Linked properties

        [XmlIgnore]
        [ScriptIgnore]
        public CmsEntityMetaField EntityMetaField
        {
            get
            {
                if (_entityMetaField == null)
                {
                    var businessFacade = new BusinessServiceFacade<CmsEntityMetaField, int>();
                    if (businessFacade != null)
                    {
                        _entityMetaField = businessFacade.FindOne(EntityMetaFieldId);
                    }
                }

                return _entityMetaField;
            }
            set
            {
                EntityMetaFieldId = value != null ? value.Id : 0;

                if (_entityMetaField != null)
                {
                    _entityMetaField = _entityMetaField.Id == EntityMetaFieldId ? value : null;
                }
                else
                {
                    _entityMetaField = value;
                }
            }
        }

        #endregion

        #region Constructor

        public CmsEntityMetaFieldLink()
        { }

        #endregion

        #region EntityObject override

        public override string TypeParameter
        {
            get
            {
                return "EntityMetaFieldLink";
            }
        }        

        #endregion
    }
}
